﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL = KC.Business.BlogSlinger;
using DAL = KC.DataAccess.BlogSlinger;

public partial class _Default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		BLL.LogActivity(ref Master.dc, Master.user, Request.UserHostAddress, "Viewed Default page", "", Request.Url.AbsoluteUri, (Request.UrlReferrer != null ? Request.UrlReferrer.AbsoluteUri : ""));
		rRecentPosts.DataSource = BLL.GetRecentPosts(ref Master.dc).Take(10);
		rRecentPosts.DataBind();
		if (Master.user != null)
			pnlPlea.Visible = false;
	}

	protected void rRecentPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			return;
		DAL.Post post = (DAL.Post)e.Item.DataItem;

		Image img = (Image)e.Item.FindControl("iPost");
		HyperLink hl = (HyperLink)e.Item.FindControl("hlPost");
		Label lblPost = (Label)e.Item.FindControl("lblPost");
		img.ImageUrl = BLL.GetUserImageURL(ref Master.dc, post);

		hl.NavigateUrl = "./Post.aspx?Title=" + post.FriendlyName;
		hl.Text = Server.HtmlEncode(post.Title);
		hl.ToolTip = post.Excerpt.Length > 1024 ? post.Excerpt.Substring(0, post.Excerpt.Length - 5) + "[...]" : post.Excerpt;

		lblPost.Text = Server.HtmlEncode(post.Excerpt.Length >= 512 ? post.Excerpt.Substring(0, post.Excerpt.Length - 5) + "[...]" : post.Excerpt);

		HyperLink hlFacebook = (HyperLink)e.Item.FindControl("hlFacebook");
		DAL.User user = BLL.GetUserByPost(ref Master.dc, post);
		hlFacebook.NavigateUrl = BLL.GetUserFacebookURL(ref Master.dc, user.ID);

		HyperLink hlBlog = (HyperLink)e.Item.FindControl("hlBlog");
		hlBlog.NavigateUrl = "Blog.aspx?ID=" + post.BlogID;
		hlBlog.Text = "More from " + post.Blog.Name + "...";
	}
}